package com.hyp.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hyp.mapper.ProductMapper;
import com.hyp.pojo.PageBean;
import com.hyp.pojo.Product;
import com.hyp.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ProductServicelmpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;

//    @Override
//    public List<Product> list() {
//        return productMapper.list();
//    }

//    @Override
//    public PageBean page(Integer page, Integer pageSize) {
//        Integer count = productMapper.count(); //获取总记录数
//        Integer start = (page - 1) * pageSize; //计算起始索引，公式:(页码-2)*页大小
//       List<Product> productList = productMapper.page(page,pageSize);
//       PageBean pageBean = new PageBean(count,productList); //封装PageBean
//       return pageBean;
//    }



// 分页查询
    @Override
    public PageBean page(Integer page,Integer pageSize,String category,String description,Integer low,Integer high){
        //设置分页参数
        PageHelper.startPage(page,pageSize);
        //执行查询
        List<Product> productList = productMapper.list(category,description,low,high);
        Page<Product> p = (Page<Product>) productList;
        //封装PageBean对象
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;

    }

    @Override
    public Product getById(Integer id) {
        return productMapper.getById(id);
    }

    @Override
    public void update(Product product) {
        product.setUpdatedAt(LocalDateTime.now());//更新修改时间为当前时间
        productMapper.update(product);
    }

    @Override
    public void addSave(Product product) {
        product.setCreatedAt(LocalDateTime.now());
        product.setUpdatedAt(LocalDateTime.now());
        productMapper.addSave(product);
    }

    @Override
    public void delete(List<Integer> ids) {
        productMapper.delete(ids);
    }

    @Override
    public List<String> getAllCategories() {
        return productMapper.getAllCategories();
    }

    @Override
    public List<Product> catelist(String category) {
        return productMapper.catelist(category);
    }


}
